package com.agung.wonogirimaps;

import java.util.List;

import org.json.JSONObject;

import com.agung.wonogirimaps.adapter.RumahMakanAdapter;
import com.agung.wonogirimaps.entity.Hotel;
import com.agung.wonogirimaps.entity.RumahMakan;
import com.agung.wonogirimaps.util.JSONAdapter;
import com.agung.wonogirimaps.util.Utils;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.ProgressDialog;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class RumahMakanActivity extends ActionBarActivity implements
		OnItemClickListener
{
	private ListView			lv;
	private JSONAdapter			json;
	private RumahMakanAdapter	adapter;
	private ProgressDialog		pDialog;

	public static final String	TAG_NAMA	= "nama";
	public static final String	TAG_ALAMAT	= "alamat";
	public static final String	TAG_MENU	= "menu";
	public static final String	TAG_LAT		= "lat";
	public static final String	TAG_LNG		= "lng";
	public static final String	TAG_GAMBAR	= "gambar";

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_rumah_makan);

		lv = (ListView) findViewById(R.id.list_rumahmakan);

		lv.setOnItemClickListener(this);

		getSupportActionBar().setTitle("Daftar Rumah Makan");
		getSupportActionBar().setIcon(android.R.color.transparent);

		json = new JSONAdapter();

		new AsyncRumahMakan().execute(Utils.API_RUMAH_MAKAN);

	}

	private class AsyncRumahMakan extends
			AsyncTask<String, Void, List<RumahMakan>>
	{
		@Override
		protected List<RumahMakan> doInBackground(String... arg0)
		{
			JSONObject obj = json.getJSONFromURL(arg0[0]);

			return json.getAllRumahMakan(obj);
		}

		@Override
		protected void onPostExecute(List<RumahMakan> result)
		{
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			pDialog.dismiss();

			adapter = new RumahMakanAdapter(RumahMakanActivity.this, result);
			lv.setAdapter(adapter);

		}

		@Override
		protected void onPreExecute()
		{
			// TODO Auto-generated method stub
			super.onPreExecute();

			pDialog = new ProgressDialog(RumahMakanActivity.this);
			pDialog.setMessage("Loading");
			pDialog.setCancelable(true);
			pDialog.show();
		}
	}

	@Override
	public void onBackPressed()
	{
		// TODO Auto-generated method stub
		super.onBackPressed();
		startActivity(new Intent(this, MainActivity.class));
		finish();
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long id)
	{
		// TODO Auto-generated method stub

		Bundle bundle = new Bundle();
		bundle.putString(TAG_NAMA,
				((RumahMakan) adapter.getItem(pos)).getNama());
		bundle.putString(TAG_ALAMAT,
				((RumahMakan) adapter.getItem(pos)).getAlamat());
		bundle.putString(TAG_MENU,
				((RumahMakan) adapter.getItem(pos)).getMenu());
		Utils.TRACE("RumahMakan", ((RumahMakan) adapter.getItem(pos)).getMenu());
		bundle.putDouble(TAG_LAT, ((RumahMakan) adapter.getItem(pos)).getLat());
		bundle.putDouble(TAG_LNG, ((RumahMakan) adapter.getItem(pos)).getLng());
		bundle.putString(TAG_GAMBAR,
				((RumahMakan) adapter.getItem(pos)).getImage());

		Intent i = new Intent(this, DetailRumahMakanActivity.class);
		i.putExtras(bundle);
		startActivity(i);

	}

}
